﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using System.Diagnostics;


namespace FileLib
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class FileOperation
    { 
        /// <summary>
        /// 将<see cref="sourceFileName"/>拷贝到<see cref="destFileName"/>
        /// </summary>
        /// <param name="sourceFileName">源文件</param>
        /// <param name="destFileName">目的文件</param>
        public static void CopyFile(string sourceFileName, string destFileName)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sourceFileName) ||
                    string.IsNullOrWhiteSpace(destFileName))
                    return;

                File.Copy(sourceFileName, destFileName, true);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="fileDirectory">目录路径</param>
        /// <returns>返回真表示成功</returns>
        public static bool CreatDirectory(string fileDirectory)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(fileDirectory))
                    return false;

                bool filedir = false;
                if (!Directory.Exists(fileDirectory))
                {
                    Directory.CreateDirectory(fileDirectory);
                    filedir = true;
                }
                else
                {
                    filedir = true;
                }

                return filedir;
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return false;
            }
        }

        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static bool IsExistFile(string filePath)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(filePath))
                    return false;

                return File.Exists(filePath);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return false;
            }
        }

        /// <summary>
        /// 返回指定路径字符串的文件名和扩展名
        /// </summary>
        /// <param name="filePath">从其获取文件名和扩展名的路径字符串</param>
        /// <returns>一个 System.String，由 path 中最后的目录字符后的字符组成。
        /// 如果 path 的最后一个字符是目录或卷分隔符，则此方法返回 System.String.Empty。
        /// 如果path 为 null，则此方法返回 null。</returns>
        public static string GetFileName(string filePath)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(filePath))
                    return string.Empty;

                return Path.GetFileName(filePath);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return string.Empty;
            }
        }

        /// <summary>
        /// 返回指定目录中的文件名称
        /// </summary>
        /// <param name="fileDirectory">目录路径</param>
        /// <returns></returns>
        public static string[] GetFiles(string fileDirectory)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(fileDirectory))
                    return null;

                string[] fileList = Directory.GetFiles(fileDirectory);
                if (fileList != null && fileList.Length > 0)
                {
                    for (int i = 0; i < fileList.Length; i++)
                    {
                        fileList[i] = Path.GetFileName(fileList[i]);
                    }

                    return fileList;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }

            return null;
        }

        /// <summary>
        /// 返回指定目录中的目录名称
        /// </summary>
        /// <param name="fileDirectory">目录路径</param>
        /// <returns></returns>
        public static string[] GetDirectories(string fileDirectory)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(fileDirectory))
                    return null;

                string[] dirList = Directory.GetDirectories(fileDirectory);
                if (dirList != null && dirList.Length > 0)
                {
                    for (int i = 0; i < dirList.Length;i++ )
                    {
                        dirList[i] = Path.GetFileName(dirList[i]);
                    }

                    return dirList;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }

            return null;
        }
    }
}
